package com.cpr.demo.mycraft.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;

/**
 * @author Chen Panrui
 * created at 2021/9/16 14:49
 */
//@Component
public class MyBeanPostProcessor implements BeanPostProcessor, Ordered {

	private final String SPECIAL_HANDLE_MYSERVICE = "myService";

	@Override
	public int getOrder() {
		return 0;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if(SPECIAL_HANDLE_MYSERVICE.equalsIgnoreCase(beanName)){
			System.out.printf("---我们在%s房间里添加了名为Seven的狗\n", beanName);
			((MyService) bean).getRoom().put("dog", "Seven");
		}
		return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if(SPECIAL_HANDLE_MYSERVICE.equalsIgnoreCase(beanName)){
			System.out.printf("---我们在%s房间里添加了名为Elizabeth的猫\n", beanName);
			((MyService) bean).getRoom().put("cat", "Elizabeth");
		}
		return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
	}
}
